package com.spring.statemachine.template;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mayixiang
 * @date 2021-02-20 10:18
 */
public class TemplateFactory {


    private final static String FIND_TEMPLATE = "/template";
    private final static String TEMPLATE_SUFFIX = "template.json";
    private final static String TEMPLATE_PROCESS = "process";
    private final static String TEMPLATE_TASK = "task";

    private final static Map<String, Process> processMap = new HashMap<>();

    static {
        loadTemplate();
    }

    /**
     * 加载模板
     */
    private static void loadTemplate() {
        String path = TemplateFactory.class.getResource(FIND_TEMPLATE).getPath();
        File file = new File(path);
        File[] files = file.listFiles();
        if (files != null){
            for (File listFile : files) {
                if (listFile.getName().lastIndexOf(TEMPLATE_SUFFIX) > 0){
                    try {
                        String templateName = listFile.getName().replace(TEMPLATE_SUFFIX, "");
                        String s = new String(Files.readAllBytes(listFile.toPath()));
                        String json = s.replace("\r\n", "").replace(" ", "");
                        processMap.put(templateName, createProcessTemplate(json));
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 创建Process模板
     * @param json 配置
     */
    private static Process createProcessTemplate(String json) throws Exception {
        JSONObject jsonObject = JSON.parseObject(json);
        JSONObject processJson = jsonObject.getJSONObject(TEMPLATE_PROCESS);
        Process process = new Process();
        process.setTask(getProcessTaskTemplateMap(processJson));
        return process;
    }

    /**
     * 创建ProcessTaskTemplate.
     * @param process 配置
     */
    private static Map<String, ProcessTask> getProcessTaskTemplateMap(JSONObject process)
        throws Exception {
        List<ProcessTask>
            task = JSONArray.parseArray(process.getJSONArray(TEMPLATE_TASK).toJSONString(), ProcessTask.class);
        Map<String, ProcessTask> processTaskMap = new HashMap<>(8);
        for (ProcessTask processTask : task) {
            if (processTaskMap.get(processTask.getId()) != null){
                throw new Exception("重复的task processId:"+processTask.getId());
            }
            processTaskMap.put(processTask.getId(), processTask);
        }
        return processTaskMap;
    }

    public static Process getProcessTemplate(ProcessTemplateEnum template){
        return processMap.get(template.getId());
    }

    @Getter
    public enum ProcessTemplateEnum{
        /**
         * 模板类型
         */
        T0_AUTO("t0_auto", "T0 自动估值模板"),
        T1_AUTO("t1_auto", "T1 自动估值模板");
        private String id;
        private String memo;
        ProcessTemplateEnum(String id, String memo){
            this.id = id;
            this.memo = memo;
        }
    }

}
